package br.com.escola.aluno.dao;

import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import br.com.escola.aluno.dto.Aluno;
import br.com.util.JdbcDao;

@Repository
public class AlunoDAOImpl extends JdbcDao implements AlunoDAO {

	@Resource
	private transient DataSource dataSource;
	
	@Override
	public DataSource getDataSource() {
		// TODO Auto-generated method stub
		return dataSource;
	}
	
	
	public void adicionarAluno(Aluno aluno) {
		String sql = "insert into aluno (nome_aluno, data_nasc_aluno, sexo_aluno) values (:nome, :dataNascimento, :sexo)";
		SqlParameterSource params = new BeanPropertySqlParameterSource(aluno);
		getJdbcTemplate().update(sql, params);
	}

	public void removerAluno(Aluno aluno) {
		String sql = "delete from aluno where id_aluno = ?";
		SqlParameterSource params = new BeanPropertySqlParameterSource(aluno);
		getJdbcTemplate().update(sql, params);
	}
	

	@Override
	public void alterarAluno(Aluno aluno) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public Aluno buscaAlunoPorId(Long id) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public List<Aluno> listarAlunos() {
		String sql = "select id_aluno as id, nome_aluno as nome, data_nasc_aluno as dataNascimento, sexo_aluno as sexo from aluno ORDER BY id_aluno ASC";
		return getJdbcTemplate().query(sql, new MapSqlParameterSource(), new BeanPropertyRowMapper<Aluno> (Aluno.class));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.escola.aluno.dao.AlunoDAO#adicionar(br.com.escola.aluno.dto.Aluno)
	 */
	/*
	 * public void adicionar(Aluno aluno) { String sql =
	 * "insert into aluno (nome_aluno, data_nasc_aluno, sexo_aluno) values (?,?,?)"
	 * ; PreparedStatement stmt; try { stmt = connection.prepareStatement(sql);
	 * stmt.setString(1, aluno.getNome()); stmt.setDate(2,
	 * aluno.getDataNascimento() != null ? new
	 * Date(aluno.getDataNascimento().getTime()) : null); stmt.setString(3,
	 * aluno.getSexo()); stmt.execute(); stmt.close(); } catch (SQLException e)
	 * { throw new RuntimeException(e); } }
	 * 
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.escola.aluno.dao.AlunoDAO#remover(br.com.escola.aluno.dto.Aluno)
	 * 
	 * public void remover(Aluno aluno) { String sql =
	 * "delete from aluno where id_aluno = ?"; PreparedStatement stmt; try {
	 * stmt = connection.prepareStatement(sql); stmt.setLong(1, aluno.getId());
	 * stmt.execute(); stmt.close(); } catch (SQLException e) { throw new
	 * RuntimeException(e); } }
	 * 
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.escola.aluno.dao.AlunoDAO#alterar(br.com.escola.aluno.dto.Aluno)
	 * 
	 * public void alterar(Aluno aluno) { String sql =
	 * "update aluno set nome_aluno = ?, data_nasc_aluno = ?, sexo_aluno = ? where id_aluno = ?"
	 * ; PreparedStatement stmt = null; try { stmt =
	 * connection.prepareStatement(sql); stmt.setString(1, aluno.getNome());
	 * stmt.setDate(2, aluno.getDataNascimento() != null ? new
	 * Date(aluno.getDataNascimento().getTime()) : null); stmt.setString(3,
	 * aluno.getSexo()); stmt.setLong(4, aluno.getId()); stmt.execute(); }catch
	 * (Exception e) { e.getLocalizedMessage(); } finally{ try { stmt.close(); }
	 * catch (SQLException e) { e.getLocalizedMessage(); } } }
	 * 
	 * (non-Javadoc)
	 * 
	 * @see br.com.escola.aluno.dao.AlunoDAO#listar()
	 * 
	 * public List<Aluno> listar() { try { List<Aluno> alunos = new
	 * ArrayList<Aluno>(); PreparedStatement stmt =
	 * this.connection.prepareStatement
	 * ("select * from aluno ORDER BY id_aluno ASC"); ResultSet rs =
	 * stmt.executeQuery(); while(rs.next()) { alunos.add(populaAluno(rs)); }
	 * rs.close(); stmt.close();
	 * 
	 * return alunos; } catch (SQLException e) { throw new RuntimeException(e);
	 * } }
	 * 
	 * (non-Javadoc)
	 * 
	 * @see br.com.escola.aluno.dao.AlunoDAO#buscaPorId(java.lang.Long)
	 * 
	 * public Aluno buscaPorId(Long id) { try { PreparedStatement stmt =
	 * this.connection
	 * .prepareStatement("select * from aluno where id_aluno = ?");
	 * stmt.setLong(1, id); ResultSet rs = stmt.executeQuery(); if(rs.next()) {
	 * return populaAluno(rs); } rs.close(); stmt.close(); return null; } catch
	 * (SQLException e) { throw new RuntimeException(e); } }
	 * 
	 * private Aluno populaAluno(ResultSet rs) throws SQLException { Aluno aluno
	 * = new Aluno(); Calendar data = Calendar.getInstance();
	 * aluno.setId(rs.getLong("id_aluno"));
	 * aluno.setNome(rs.getString("nome_aluno"));
	 * data.setTime(rs.getDate("data_nasc_aluno"));
	 * aluno.setDataNascimento(data.getTime());
	 * aluno.setSexo(rs.getString("sexo_aluno")); return aluno; }
	 */
}
